<%@page language="java" contentType="text/html; charset=utf-8"%>

<%@page import="java.util.*"%>
<%@page import="com.ibm.eg.cvmanager.dto.*"%>
<%@page import="com.ibm.eg.cvmanager.business.stub.*"%>
<%@page import="com.ibm.eg.cvmanager.business.interfaces.*"%>

<html>
<head>
<script type="text/javascript">
	function del() {
		var xmlhttp;
		
		if (window.XMLHttpRequest) {
			xmlhttp=new XMLHttpRequest();
		} else {
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				document.getElementById("searchResultDiv").innerHTML = xmlhttp.responseText;
			} else {
				document.getElementById("searchResultDiv").innerHTML = "<b>Runtime error code = " + xmlhttp.status + ", please contact the site owner</b>";
			}

			document.getElementById("searchButton").disabled = false;
		};

		var post = "";
		
		for(cnt = 0;cnt < document.getElementById("resultForm").elements.length;cnt++) {
		    if("delete" == document.getElementById("resultForm").elements[cnt].name) {
				if (document.getElementById("resultForm").elements[cnt].checked) {
					if(post != "")
						post += "&";
					
					post += "delete=" + document.getElementById("resultForm").elements[cnt].value;
				}
		    }
		}
		
		xmlhttp.open("POST", "deleteInterview.jsp", true);
		xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		
		document.getElementById("searchResultDiv").innerHTML = "<b>Deleting ...</b>";		
		document.getElementById("searchButton").disabled = true;
					
		xmlhttp.send(post);
	}

	function search() {
		var xmlhttp;
		
		if (window.XMLHttpRequest) {
			xmlhttp=new XMLHttpRequest();
		} else {
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				document.getElementById("searchResultDiv").innerHTML=xmlhttp.responseText;
			} else {
				document.getElementById("searchResultDiv").innerHTML = "<b>Runtime error code = " + xmlhttp.status + ", please contact the site owner</b>";
			}
			
			document.getElementById("searchButton").disabled = false;
		};
		
		var state = document.getElementById("state").value;

		var from_day = document.getElementById("from_day").value;
		var from_month = document.getElementById("from_month").value;
		var from_year = document.getElementById("from_year").value;
		var to_day = document.getElementById("to_day").value;
		var to_month = document.getElementById("to_month").value;
		var to_year = document.getElementById("to_year").value;
		
		xmlhttp.open("POST", "searchResult.jsp", true);
		xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		
		document.getElementById("searchResultDiv").innerHTML = "<b>Searching ...</b>";
		document.getElementById("searchButton").disabled = true;
		
		
		xmlhttp.send(	"state" + "=" + state 
				+ "&" + "from_day" + "=" + from_day
				+ "&" + "from_month" + "=" + from_month
				+ "&" + "from_year" + "=" + from_year
				+ "&" + "to_day" + "=" + to_day
				+ "&" + "to_month" + "=" + to_month
				+ "&" + "to_year" + "=" + to_year);
	}
</script>
</head>

<body>

<%
InterviewBusiness business = new InterviewBusinessImpl();

String id = request.getParameter("id");

if(id != null) {
	Interview interview = new Interview();
	
	interview.setComment(request.getParameter("comment"));
	
	int day = Integer.parseInt(request.getParameter("day"));
	int month = Integer.parseInt(request.getParameter("month"));
	int year = Integer.parseInt(request.getParameter("year")) - 1900;
	
	interview.setInterviewdate(new Date(year, month, day));
	interview.setStatus(Integer.parseInt(request.getParameter("interview_state")));
	interview.setComment(request.getParameter("comment"));

	if(id.equals("null")) {
		business.addInterview(interview);
	} else {
		interview.setId(Integer.parseInt(id));
		business.updateInterview(interview);
	}
	
	session.removeAttribute("interview");
}
%>

	<table align="center">
		<tr>
			<td>State:</td>
			<td>
				<SELECT id="state" NAME="state">
					<OPTION VALUE="none">None</OPTION>
					<OPTION VALUE="SUCCEEDED">Pass</OPTION>
					<OPTION VALUE="FAILED">Fail</OPTION>
					<OPTION VALUE="UNKNOWN">Unknown</OPTION>
				</SELECT>
			</td>
		</tr>	
		<tr>
			<td>From:</td>
			<td>Day:</td>
			<td>
				<select id="from_day" name="from_day">
				<% for(int i = 1;i <= 31;i++) { %>
					<option value="<%=i%>"><%=i %></option>
				<%} %>
				</select>
			</td>
			<td>Month:</td>
			<td>
				<select id="from_month" name="from_month">
				<% for(int i = 1;i <= 12;i++) { %>
					<option value="<%=i%>"><%=i %></option>
				<%} %>
				</select>
			</td>
			<td>Year:</td>
			<td><input id="from_year" name="from_year" type="text" size="4"> </td>
		</tr>
		<tr>
			<td>To:</td>
			<td>Day:</td>
			<td>
				<select id="to_day" name="to_day">
				<% for(int i = 1;i <= 31;i++) { %>
					<option value="<%=i%>"><%=i %></option>
				<%} %>
				</select>
			</td>
			<td>Month:</td>
			<td>
				<select id="to_month" name="to_month">
				<% for(int i = 1;i <= 12;i++) { %>
					<option value="<%=i%>"><%=i %></option>
				<%} %>
				</select>
			</td>
			<td>Year:</td>
			<td><input id="to_year" name="to_year" type="text" size="4"></td>
		</tr>
	</table>
	
	<table align="center">
		<tr>
			<td>
				<form>
					<button id="searchButton" type="button" onclick="search();">Search</button>
				</form>
			</td>
			
			<td>
				<form method="post" action="addpage.jsp">
					<input type="submit" name="add" value="Add"/>
				</form>
			</td>
		</tr>
	</table>
	
	<table align="center">
		<tr>
			<td>
				<div id="searchResultDiv" align="center"></div>
			</td>
		</tr>
	</table>
</body>
</html>